var content = document.querySelector('.content');
var tbody = document.querySelector(".grade-table tbody");
var searchBtn = document.getElementById('searchBtn');
var tui = document.querySelector('.tui');

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data = [];
//test
var test_data = [
  {
    Cou_id: 2,
    Cou_name: "课程2",
    Cou_major: "专业2",
    Cou_time: "时间2",
    Tea_name: "教师2",
    Cou_payment: "1000",
  }
];
data = test_data;

document.addEventListener("DOMContentLoaded", function() {

    $.ajax({
      url: '/management/stuservlet/searchCoursesNoPaid',                                 //url方法接口：获取需要缴费课程
      method: 'POST',
      data: {
        username: "1"
      },
      success: function(response) {
        console.log(response);
        console.log(response.data);
        data = response.data;

        //test
        //data = test_data;

        if(data.length == 0){
            tui.style.display = 'block';
        }
        else{
            tui.style.display = 'none';
            // 将数据数组赋值给相应的元素
            var currentIndex = 1;
            data.forEach(function(item) {
              var row = document.createElement("tr");                       //持保留，不知道啥是课程钱
              row.innerHTML = `
                <td>${currentIndex}</td>
                <td>${item.Cou_id}</td>
                <td>${item.Cou_name}</td>
                <td>${item.Cou_time}</td>
                <td>${item.Cou_teacher}</td>
                <td>${item.Cou_payment}</td>
                <td><button id="jjw${currentIndex}">缴费</button></td>
              `;
              tbody.appendChild(row);
              currentIndex++;
             // console.log(jjw${currentIndex});
            });
            for (let i=1; i < currentIndex; i++) {
                  var jjw = document.getElementById('jjw'+i);
                //  console.log(jjw);
                  jjw.addEventListener('click', function() {
                    // 编写跳转到修改信息页面的操作
                    // window.location.href = "editstupwd.html";
                      var confirmLogout = confirm("确定要缴费吗？");
                        if (confirmLogout) {
                          // 用户点击了确认按钮
                          // 编写退出登录的操作
                          $.ajax({
                                url: '/management/stuservlet/savePayment',                              //url方法接口：交钱了
                                method: 'POST',
                                data: {
                                  count: i
                                },
                                success: function(response) {
                                  console.log(response);
                                  console.log(response.data);
                                  alert("缴费成功！");
                                  window.location.href = "stulogin_pay.html"
                                }
                              });
                        } else {
                          // 用户点击了取消按钮
                          // 可以选择执行其他操作或不进行任何操作
                        }
                  });
                }
            }
        }
    });

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }
});

$(document).keydown(function(event) {
  if (event.keyCode == 13) { // 13代表回车键的键码
    $('#searchBtn').click();
  }
});

searchBtn.addEventListener('click', function() {                            //持保留。可以此功能去除，先不着急弄
    var searchInput = document.getElementById("search");
    var searchText = searchInput.value;
    $.ajax({
        url: '/management/stuservlet/searchByCouNameEX',                                 //url方法接口：搜索
        method: 'POST',
        data: {
            searchText: searchText
        },
        success: function(response) {
            console.log(response);
            var search = response.data;
            console.log(search);
            if(search.length == 0){
                alert("没有符合要求的课程！");
            }
            else{
            tbody.innerHTML = "";
            currentIndex = 1;
            search.forEach(function(item) {
                var row = document.createElement("tr");
                row.innerHTML = `
                    <td>${currentIndex}</td>
                    <td>${item.Cou_id}</td>
                    <td>${item.Cou_name}</td>
                    <td>${item.Cou_time}</td>
                    <td>${item.Cou_teacher}</td>
                    <td>${item.Cou_payment}</td>
                    <td><button id="jjw${currentIndex}">缴费</button></td>
                `;
                tbody.appendChild(row);
                currentIndex++;
            });
            for (let i = 1; i < currentIndex; i++) {
                var jjw = document.getElementById('jjw' + i);
                jjw.addEventListener('click', function() {
                    var confirmLogout = confirm("确定要缴费吗？");
                    if (confirmLogout) {
                        $.ajax({
                            url: '/management/stuservlet/savePaymentEX',                             //url方法接口：交钱了
                            method: 'POST',
                            data: {
                                count: i
                            },
                            success: function(response) {
                                console.log(response);
                                console.log(response.data);
                                alert("缴费成功！");
                                window.location.href = "stulogin_pay.html"

                            }
                        });
                    } else {
                        // 用户点击了取消按钮
                        // 可以选择执行其他操作或不进行任何操作
                    }
                });
            }
            }
        }
    });
});
